home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / libraries / newiff.lha / NewIFF / NewIFF39.lha / newiff39 / apps / 24bitDemo / 24BitDemo.c < prev    next >
C/C++ Source or Header  |  1993-09-28  |  11KB  |  426 lines

  1. /* 24bitDemo.c 05/91  C. Scheppner CBM
  2.  *
  3.  * Example which creates a 24-bit raster, saves it as a 24-bit ILBM,
  4.  *   then loads it as a brush and shows it to you 4 planes at a time
  5.  *   Optionally (if given a filename) just displays 4 planes at a time.
  6.  *
  7.  * requires linkage with several IFF modules
  8.  * see Makefile
  9.  *
  10.  * 37.10 07/92 - use scr->RastPort.BitMap instead of &scr->BitMap
  11.  *            for future compatibility
  12.  *
  13.  */
  14. #define INTUI_V36_NAMES_ONLY
  15.  
  16. #include "iffp/ilbmapp.h"
  17.  
  18.  
  19. #ifdef __SASC
  20. void __chkabort(void) {}          /* Disable SAS CTRL-C checking. */
  21. #else
  22. #ifdef LATTICE
  23. void chkabort(void) {}            /* Disable LATTICE CTRL-C checking */
  24. #endif
  25. #endif
  26.  
  27.  
  28. void cleanup(void);
  29. void bye(UBYTE *s,int error);
  30.  
  31. #define MINARGS 1
  32.  
  33. #include "24bitdemo_rev.h"
  34. UBYTE vers[] = VERSTAG;
  35. UBYTE Copyright[] = VERS " - Freely Redistributable";
  36.  
  37. char *usage = "Usage: 24bitDemo [loadname] (saves/loads if no loadname given)";
  38.  
  39.  
  40. struct Library *IntuitionBase  = NULL;
  41. struct Library *GfxBase        = NULL;
  42. struct Library *IFFParseBase   = NULL;
  43.  
  44. /* Note - these fields are also available in the ILBMInfo structure */
  45. struct   Screen         *scr;         /* for ptr to screen structure */
  46. struct   Window         *win;         /* for ptr to window structure */
  47. struct   RastPort       *wrp;         /* for ptr to RastPort  */
  48. struct   ViewPort       *vp;          /* for ptr to Viewport  */
  49.  
  50.  
  51. struct   NewWindow      mynw = {
  52.    0, 0,                                  /* LeftEdge and TopEdge */
  53.    0, 0,                                /* Width and Height */
  54.    (UBYTE)-1, (UBYTE)-1,                  /* DetailPen and BlockPen */
  55.    IDCMP_VANILLAKEY | IDCMP_MOUSEBUTTONS, /* IDCMP Flags with Flags below */
  56.    WFLG_BACKDROP | WFLG_BORDERLESS |
  57.    WFLG_SMART_REFRESH | WFLG_NOCAREREFRESH |
  58.    WFLG_ACTIVATE | WFLG_RMBTRAP,
  59.    NULL, NULL,                            /* Gadget and Image pointers */
  60.    NULL,                                  /* Title string */
  61.    NULL,                                  /* Screen ptr null till opened */
  62.    NULL,                                  /* BitMap pointer */
  63.    50, 20,                                /* MinWidth and MinHeight */
  64.    0 , 0,                                 /* MaxWidth and MaxHeight */
  65.    CUSTOMSCREEN                           /* Type of window */
  66.    };
  67.  
  68.  
  69. BOOL   FromWb;
  70.  
  71.  
  72. /* ILBM Property chunks to be grabbed
  73.  * List BMHD, CMAP and CAMG first so we can skip them when we write
  74.  * the file back out (they will be written out with separate code)
  75.  */
  76. LONG    ilbmprops[] = {
  77.         ID_ILBM, ID_BMHD,
  78.         ID_ILBM, ID_CMAP,
  79.         ID_ILBM, ID_CAMG,
  80.         ID_ILBM, ID_CCRT,
  81.         ID_ILBM, ID_AUTH,
  82.         ID_ILBM, ID_Copyright,
  83.         TAG_DONE
  84.         };
  85.  
  86. /* ILBM Collection chunks (more than one in file) to be gathered */
  87. LONG    ilbmcollects[] = {
  88.         ID_ILBM, ID_CRNG,
  89.         TAG_DONE
  90.         };
  91.  
  92. /* ILBM Chunk to stop on */
  93. LONG    ilbmstops[] = {
  94.         ID_ILBM, ID_BODY,
  95.         TAG_DONE
  96.         };
  97.  
  98.  
  99. UBYTE nomem[]  = "Not enough memory\n";
  100. UBYTE noiffh[] = "Can't alloc iff\n";
  101.  
  102.  
  103. /* For our allocated ILBM frames */
  104. struct ILBMInfo  *ilbm[2];
  105.  
  106. #define SCRPLANES 4
  107.  
  108. USHORT colortable[32];
  109. USHORT cstarts[]= { 0x000, 0x800, 0x000, 0x080, 0x000, 0x008 };
  110. USHORT coffs[]    = { 0x100, 0x100, 0x010, 0x010, 0x001, 0x001 };
  111.  
  112. UBYTE *ilbmname = "RAM:24bit.ilbm";
  113. UBYTE *rgbnames[]={"R0","R1","R2","R3","R4","R5","R6","R7",
  114.            "G0","G1","G2","G3","G4","G5","G6","G7",
  115.            "B0","B1","B2","B3","B4","B5","B6","B7" };
  116.  
  117. UBYTE *endtext1 = "Displayed 24 planes, 4 at a time.";
  118. UBYTE *endtext2 = "Press mousebutton or key to exit.";
  119.  
  120. /* 
  121.  * MAIN 
  122.  */
  123. void main(int argc, char **argv)
  124.    {
  125.     struct RastPort *rp = NULL;
  126.     struct BitMap dummy = {0};
  127.     struct BitMap *bm = NULL, *xbm, *sbm;
  128.     LONG    error = 0L;
  129.     USHORT    width, height, depth, pwidth, pheight, pmode, extra, rgb;
  130.     ULONG     plsize;
  131.     UBYTE    *tpp;
  132.     BOOL    DoSave = TRUE;
  133.     int     k, p, s, n;
  134.  
  135.    FromWb = argc ? FALSE : TRUE;
  136.  
  137.    if((argc > 1)&&(argv[argc-1][0]=='?'))
  138.     {
  139.     printf("%s\n%s\n",Copyright,usage);
  140.         bye("",RETURN_OK);
  141.     }
  142.  
  143.    if(argc==2)
  144.     {
  145.     ilbmname = argv[1];
  146.     DoSave = FALSE;
  147.     }
  148.  
  149.    /* Open Libraries */
  150.  
  151.    if(!(IntuitionBase = OpenLibrary("intuition.library", 0)))
  152.       bye("Can't open intuition library.\n",RETURN_WARN);
  153.       
  154.    if(!(GfxBase = OpenLibrary("graphics.library",0)))
  155.       bye("Can't open graphics library.\n",RETURN_WARN);
  156.  
  157.    if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  158.       bye("Can't open iffparse library.\n",RETURN_WARN);
  159.  
  160.  
  161. /* 
  162.  * Alloc ILBMInfo structs
  163.  */
  164.     if(!(ilbm[0] = (struct ILBMInfo *)
  165.     AllocMem(sizeof(struct ILBMInfo),MEMF_PUBLIC|MEMF_CLEAR))) 
  166.         bye(nomem,RETURN_FAIL);
  167.     if(!(ilbm[1] = (struct ILBMInfo *)
  168.     AllocMem(sizeof(struct ILBMInfo),MEMF_PUBLIC|MEMF_CLEAR))) 
  169.         bye(nomem,RETURN_FAIL);
  170.  
  171. /*
  172.  * Here we set up our ILBMInfo fields for our
  173.  * application.
  174.  * Above we have defined the propery and collection chunks
  175.  * we are interested in (some required like BMHD)
  176.  */
  177.  
  178.     ilbm[0]->ParseInfo.propchks        = ilbmprops;
  179.     ilbm[0]->ParseInfo.collectchks    = ilbmcollects;
  180.     ilbm[0]->ParseInfo.stopchks        = ilbmstops;
  181.  
  182.     ilbm[0]->windef    = &mynw;
  183.  
  184.     *ilbm[1] = *ilbm[0];
  185.  
  186.  
  187. /* 
  188.  * Alloc IFF handles for frame
  189.  */
  190.     if(!(ilbm[0]->ParseInfo.iff = AllocIFF())) bye(noiffh,RETURN_FAIL);
  191.     if(!(ilbm[1]->ParseInfo.iff = AllocIFF())) bye(noiffh,RETURN_FAIL);
  192.  
  193.  
  194. /* for saving our demo 24-bit ILBM */
  195.  
  196.     width  = 320;
  197.     height = 200;
  198.     depth  = 24;
  199.  
  200.     /* Page width, height, and mode for saved ILBM */
  201.     pwidth  = width  < 320 ? 320 : width;
  202.     pheight = height < 200 ? 200 : height;
  203.     pmode   = pwidth >= 640  ? HIRES : 0L;
  204.     pmode  |= pheight >= 400 ? LACE  : 0L;
  205.  
  206.     plsize = RASSIZE(width,height);
  207.  
  208.     if(!DoSave)    goto nosave;
  209.  
  210.     /*
  211.      * Allocate Bitmap and planes
  212.      */
  213.     extra = depth > 8 ? depth - 8 : 0;
  214.     if(ilbm[0]->brbitmap = AllocMem(sizeof(struct BitMap) + (extra<<2),
  215.                 MEMF_CLEAR))
  216.     {
  217.     bm = ilbm[0]->brbitmap;
  218.         InitBitMap(bm,depth,width,height);
  219.         for(k=0, error=0; k<depth && (!error); k++) 
  220.             {
  221.             if(!(bm->Planes[k] = AllocRaster(width,height)))
  222.             error = IFFERR_NOMEM;
  223.             if(! error)
  224.         {
  225.                 BltClear(bm->Planes[k], RASSIZE(width,height),0);
  226.                 }
  227.         }
  228.  
  229.     if(!error)
  230.         {
  231.         if(!(rp = AllocMem(sizeof(struct RastPort),MEMF_CLEAR)))
  232.         error = IFFERR_NOMEM;
  233.         else
  234.         {
  235.         InitRastPort(rp);
  236.         rp->BitMap = bm;
  237.         rp->Mask = 0x01;    /* we'll render 1 plane at a time */
  238.         SetAPen(rp,1);
  239.         SetDrMd(rp,JAM1);
  240.         }
  241.         }
  242.  
  243.     if(!error)
  244.         {
  245.         /* Put something recognizable in the planes.
  246.          * Our bitmap is not part of a screen or viewport
  247.          * so we can fiddle with the pointers and depth
  248.          */
  249.         tpp = bm->Planes[0];    /* save first plane pointer */
  250.         bm->Depth = 1;
  251.         for(k=0; k<depth; k++)    /* swap in planeptrs 1 at a time */
  252.         {
  253.         bm->Planes[0] = bm->Planes[k];
  254.         Move(rp,k * 10, (k * 8) + 8);    /* render rgb bitname text */
  255.         Text(rp, rgbnames[k], 2);
  256.         }
  257.         bm->Depth = depth;        /* restore depth */
  258.         bm->Planes[0] = tpp;    /* and first pointer */
  259.  
  260.         /* Save the 24-bit ILBM */
  261.         printf("Saving %s\n",ilbmname);
  262.         error = saveilbm(ilbm[0], ilbm[0]->brbitmap, pmode,
  263.                 width,  height, pwidth, pheight,
  264.                 NULL, 0, 0,    /* colortable */
  265.         mskNone, 0,    /* masking, transparent */
  266.         NULL, NULL,     /* chunklists */
  267.         ilbmname);
  268.         }
  269.  
  270.     /* Free our bitmap */
  271.         for(k=0; k<depth; k++) 
  272.             {
  273.             if(ilbm[0]->brbitmap->Planes[k])
  274.             FreeRaster(ilbm[0]->brbitmap->Planes[k],width,height);
  275.         }
  276.     FreeMem(ilbm[0]->brbitmap, sizeof(struct BitMap) + (extra << 2));
  277.     ilbm[0]->brbitmap = NULL;
  278.     if(rp)    FreeMem(rp, sizeof(struct RastPort));
  279.     }
  280.  
  281.     if(error)
  282.     {
  283.     printf("%s\n",IFFerr(error));
  284.     bye(" ", RETURN_FAIL);
  285.     }
  286.  
  287. nosave:
  288.  
  289. /* Normally you would use showilbm() to open an appropriate acreen
  290.  * and display an ILBM in it.  However, this is a 24-bit ILBM
  291.  * so we will load it as a brush (bitmap).
  292.  * Here we are demonstrating
  293.  *  - first querying an ILBM to get its BMHD and CAMG (real or computed)
  294.  *  - then opening our own display
  295.  *  - then loading the 24-bit ILBM as a brush (bitmap) and displaying
  296.  *    it 4 planes at a time in our 4-plane screen.
  297.  */
  298.  
  299.     printf("Attempting to load %s as a bitmap and display 4 planes at a time\n",
  300.         ilbmname);
  301.  
  302.     if(!(error = queryilbm(ilbm[0],ilbmname)))
  303.     {
  304.     D(bug("24bitDemo: after query, this ILBM is %ld x %ld x %ld, modeid=$%lx\n",
  305.         ilbm[0]->Bmhd.w, ilbm[0]->Bmhd.h, ilbm[0]->Bmhd.nPlanes, ilbm[0]->camg));
  306.  
  307.     /* Note - you could use your own routines to open your
  308.      * display, but if so, you must initialize ilbm[0]->scr,
  309.      * ilbm[0]->win, ilbm[0]->wrp, ilbm[0]->srp, and ilbm[0]->vp for your display.
  310.      * Here we will use opendisplay() which will initialize
  311.      * those fields.
  312.      */
  313.     if(!(opendisplay(ilbm[0],
  314.             MAX(ilbm[0]->Bmhd.pageWidth, ilbm[0]->Bmhd.w),
  315.             MAX(ilbm[0]->Bmhd.pageHeight,ilbm[0]->Bmhd.h),
  316.             MIN(ilbm[0]->Bmhd.nPlanes, SCRPLANES),
  317.             ilbm[0]->camg)))
  318.         {
  319.         printf("Failed to open display\n");
  320.         }
  321.     else
  322.         {
  323.         D(bug("24bitDemo: opendisplay (%ld planes) successful\n",SCRPLANES));
  324.  
  325.         scr = ilbm[0]->scr;
  326.         win = ilbm[0]->win;
  327.         wrp = ilbm[0]->wrp;
  328.         vp  = ilbm[0]->vp;
  329.  
  330.         if(!(error = loadbrush(ilbm[1], ilbmname)))
  331.         {
  332.             D(bug("24bitDemo: loadbrush successful\n"));
  333.  
  334.         /* Note - we don't need to examine or copy any
  335.          * chunks from the file, so we will close file now
  336.          */
  337.         closeifile(ilbm[0]);
  338.         ScreenToFront(ilbm[0]->scr);
  339.  
  340.         xbm = &dummy;            /* spare bitmap */
  341.         sbm = scr->RastPort.BitMap;    /* screen's bitmap */
  342.         bm = ilbm[1]->brbitmap;        /* the 24-plane bitmap */
  343.         depth = bm->Depth;
  344.  
  345.             InitBitMap(xbm,SCRPLANES,scr->Width,scr->Height);
  346.  
  347.         /* Show the 24 planes */
  348.         for(p=0; p<depth; p+=SCRPLANES)    /* 4 at a time */
  349.             {
  350.             SetRast(&scr->RastPort, 0);
  351.             for(s=0; s<SCRPLANES; s++)
  352.             {
  353.             if((p+s) < depth) xbm->Planes[s] = bm->Planes[p+s];
  354.             else            xbm->Planes[s] = NULL, xbm->Depth--;
  355.             }
  356.             /* Blit planes to the screen */
  357.             BltBitMap(xbm, 0, 0,
  358.                   sbm, 0, 0,
  359.                   scr->Width, scr->Height,
  360.                   0xC0, 0x0F, NULL);
  361.  
  362.             /* Emulate 8-bit color with 4-bit per gun colors
  363.              * by using each rgb value twice
  364.              */
  365.             for(n=0, rgb=cstarts[p /SCRPLANES]; n < 16; n++)
  366.             {
  367.             if(!n)    colortable[n] = 0xFFF;
  368.             else    colortable[n] = rgb;
  369.             /* bump gun for every 2 planes since
  370.              * we only have 8 bits per gun
  371.              */
  372.             if(n & 1)  rgb += coffs[ p / SCRPLANES];
  373.             }
  374.             LoadRGB4(vp, colortable, 16);
  375.             Delay(50);
  376.             }
  377.  
  378.         SetRast(&scr->RastPort, 0);
  379.  
  380.         SetAPen(wrp, 1);
  381.         Move(wrp, 24, 80);
  382.         Text(wrp, endtext1, strlen(endtext1));
  383.         Move(wrp, 24, 120);
  384.         Text(wrp, endtext2, strlen(endtext2));
  385.  
  386.         Wait(1<<win->UserPort->mp_SigBit);
  387.         unloadbrush(ilbm[1]);    /* deallocs colors, closeifile if needed */
  388.         }
  389.         closedisplay(ilbm[0]);
  390.         printf("Done\n");
  391.          }
  392.           }
  393.  
  394.     if(error)    printf("%s\n",IFFerr(error));
  395.  
  396.     cleanup();
  397.     exit(RETURN_OK);
  398.     }
  399.  
  400.  
  401. void bye(UBYTE *s,int error)
  402.    {
  403.    if((*s)&&(!FromWb)) printf("%s\n",s);
  404.    cleanup();
  405.    exit(error);
  406.    }
  407.  
  408.  
  409. void cleanup()
  410.    {
  411.    if(ilbm[0])
  412.     {
  413.     if(ilbm[0]->ParseInfo.iff)     FreeIFF(ilbm[0]->ParseInfo.iff);
  414.     FreeMem(ilbm[0],sizeof(struct ILBMInfo));
  415.     }
  416.    if(ilbm[1])
  417.     {
  418.     if(ilbm[1]->ParseInfo.iff)     FreeIFF(ilbm[1]->ParseInfo.iff);
  419.     FreeMem(ilbm[1],sizeof(struct ILBMInfo));
  420.     }
  421.  
  422.    if(GfxBase)              CloseLibrary(GfxBase);
  423.    if(IntuitionBase)     CloseLibrary(IntuitionBase);
  424.    if(IFFParseBase)      CloseLibrary(IFFParseBase);
  425.    }
  426.